'''
Created on Mar 1, 2013

@author: larsonmp
'''

import os
import random
import sys

columns = [
    'actual_time'
    'age'
    'community_building'
    'community_membership_family'
    'community_membership_hobbies'
    'community_membership_none'
    'community_membership_other'
    'community_membership_political'
    'community_membership_professional'
    'community_membership_religious'
    'community_membership_support'
    'country'
    'disability_cognitive'
    'disability_hearing'
    'disability_motor'
    'disability_not_impaired'
    'disability_not_say'
    'disability_vision'
    'education_attainment'
    'falsification_of_information'
    'gender'
    'household_income'
    'how_you_heard_about_survey_banner'
    'how_you_heard_about_survey_friend'
    'how_you_heard_about_survey_mailing_list'
    'how_you_heard_about_survey_others'
    'how_you_heard_about_survey_printed_media'
    'how_you_heard_about_survey_remebered'
    'how_you_heard_about_survey_search_engine'
    'how_you_heard_about_survey_usenet_news'
    'how_you_heard_about_survey_www_page'
    'major_geographical_location'
    'major_occupation'
    'marital_status'
    'most_import_issue_facing_the_internet'
    'opinions_on_censorship'
    'primary_computing_platform'
    'primary_language'
    'primary_place_of_www_access'
    'race'
    'not_purchasing_bad_experience'
    'not_purchasing_bad_press'
    'not_purchasing_cant_find'
    'not_purchasing_company_policy'
    'not_purchasing_easier_locally'
    'not_purchasing_enough_info'
    'not_purchasing_judge_quality'
    'not_purchasing_never_tried'
    'not_purchasing_no_credit'
    'not_purchasing_not_applicable'
    'not_purchasing_not_option'
    'not_purchasing_other'
    'not_purchasing_prefer_people'
    'not_purchasing_privacy'
    'not_purchasing_receipt'
    'not_purchasing_security'
    'not_purchasing_too_complicated'
    'not_purchasing_uncomfortable'
    'not_purchasing_unfamiliar_vendor'
    'registered_to_vote'
    'sexual_preference'
    'web_ordering'
    'web_page_creation'
    'who_pays_for_access_do_not_know'
    'who_pays_for_access_other'
    'who_pays_for_access_parents'
    'who_pays_for_access_school'
    'who_pays_for_access_self'
    'who_pays_for_access_work'
    'willingness_to_pay_fees'
    'years_on_internet'
    'id'
]

states = {
      0: 'Alabama',
      1: 'Alaska',
      2: 'Arizona',
      3: 'Arkansas',
      4: 'California',
      5: 'Colorado',
      6: 'Connecticut',
      7: 'Florida',
      8: 'Georgia',
      9: 'Hawaii',
     10: 'Idaho',
     11: 'Illinois',
     12: 'Indiana',
     13: 'Iowa',
     14: 'Kansas',
     15: 'Kentucky',
     16: 'Louisiana',
     17: 'Maine',
     18: 'Maryland',
     19: 'Massachusetts',
     20: 'Michigan',
     21: 'Minnesota',
     22: 'Mississippi',
     23: 'Missouri',
     24: 'Montana',
     25: 'Nebraska',
     26: 'Nevada',
     27: 'New Hampshire',
     28: 'New Jersey',
     29: 'New York',
     30: 'North Carolina',
     31: 'North Dakota',
     32: 'Ohio',
     33: 'Oklahoma',
     34: 'Oregon',
     35: 'Pennsylvania',
     36: 'Rhode Island',
     37: 'South Carolina',
     38: 'South Dakota',
     39: 'Tennessee',
     40: 'Texas',
     41: 'Utah',
     42: 'Vermont',
     43: 'Virginia',
     44: 'West Virginia',
     45: 'Wisconsin',
     46: 'Wyoming',
     47: 'Washington D.C.',
     48: 'Washington',
     70: 'Delaware',
    103: 'New Mexico'
}

provinces = {
     50: 'Ontario',
     51: 'Alberta',
     57: 'Saskatchewan',
     60: 'New Brunswick',
     63: 'British Columbia',
     66: 'Quebec',
     68: 'Manitoba',
     73: 'Newfoundland',
     89: 'Nova Scotia'
}

countries = {
     49: 'Bhutan',
     52: 'Spain',
     53: 'Austria',
     54: 'Australia',
     55: 'United Kingdom',
     56: 'South Africa',
     58: 'Italy',
     59: 'Netherlands',
     61: 'New Zealand',
     62: 'Nicaragua',
     64: 'Sweden',
     65: 'South Korea',
     67: 'Tunisia',
     69: 'Denmark',
     71: 'Algeria',
     72: 'Germany',
     74: 'Thailand',
     75: 'Switzerland',
     76: 'Belgium',
     77: 'Hong Kong',
     78: 'Norway',
     79: 'Kuwait',
     80: 'Finland',
     81: 'Russia',
     82: 'Middle East',
     83: 'Jamaica',
     84: 'Afghanistan',
     85: 'Burundi',
     86: 'France',
     87: 'Brazil',
     88: 'Turkey',
     90: 'Venezuela',
     91: 'Argentina',
     92: 'Isreal',
     93: 'Ireland',
     94: 'Colombia',
     95: 'Philippines',
     96: 'Malaysia',
     97: 'Greece',
     98: 'India',
     99: 'Singapore',
    100: 'Portugal',
    101: 'Japan',
    102: 'Taiwan',
    104: 'Namibia',
    105: 'Romania',
    106: 'Iceland',
    107: 'Hungary',
    108: 'Costa Rica',
    109: 'Panama',
    110: 'Ecuador',
    111: 'Croatia',
    112: 'Poland',
    113: 'Dominican Republic',
    114: 'Kenya',
    115: 'Sri Lanka',
    116: 'Puerto Rico',
    117: 'Indonesia',
    118: 'Czech',
    119: 'Chile',
    120: 'Egypt',
    121: 'Morocco',
    122: 'Yukon',
    123: 'China'
}

age_index = 0
location_index = 11
education_attainment_index = 18
gender_index = 20
occupation_index = 32
primary_computing_platform_index = 36
web_page_creation_index = 62
years_on_internet_index = 70
id_index = 71

class InternetUsageRecord(object):
    '''
    classdocs
    '''
    def __init__(self, record):
        '''
        Constructor
        '''
        self.__absorb(record.split())
    
    def translate_education(self, value):
        return {
            0: 'grammar',
            1: 'high school',
            2: 'professional',
            3: 'some college',
            4: 'college',
            5: 'masters',
            6: 'doctoral',
            7: 'special',
            99: 'other'}[value]
    
    def translate_internet_usage(self, value):
        return {
            0: '0.0 - 0.5 year',
            1: '0.5 - 1.0 year',
            2: '1.0 - 4.0 years',
            3: '4.0 - 7.0 years',
            4: '7.0+ years'
        }.get(value, '--')
    
    def translate_location(self, value):
        if states.has_key(value):
            return ('United States', states[value])
        elif provinces.has_key(value):
            return ('Canada', provinces[value])
        else:
            return (countries[value], '--')
    
    def translate_occupation(self, value):
        return {
            0: '--',
            1: 'Artist/Musician',
            2: 'College Student',
            3: 'College Educator',
            4: 'Consultant',
            5: 'Accountant',
            6: 'Architect',
            7: 'Advertising Professional',
            8: 'Administrator/Secretary',
            9: 'Civil Servant',
           10: 'Counselor',
           11: 'CEO',
           12: 'Engineer',
           13: 'Health Care Worker',
           14: 'Homemaker',
           15: 'K-12 Student',
           16: 'K-12 Educator',
           17: 'IS',
           18: 'Laid off',
           19: 'Looking',
           20: 'Manager',
           21: 'Marketing Professional',
           22: 'Microcomputer',
           23: 'Networking',
           24: 'Nurse',
           25: 'Performer',
           26: 'Physician',
           27: 'Programmer',
           28: 'Retired',
           29: 'Salesperson',
           30: 'Self Employed',
           31: 'Service Industry Occupation',
           32: 'Technician',
           33: 'Vice President',
           34: 'Writer/Journalist',
           35: 'Attorney/Judge',
           36: 'Broadcast/Media',
           37: 'Designer',
           38: 'Military',
           39: 'Investor',
           40: 'Entertainment',
           41: 'CIO',
           42: 'CFO',
           43: 'clerk',
           44: 'Religious Occupation',
           98: '--',
           99:'Other'
        }[value]
    
    def translate_platform(self, value):
        return {
            0:'DOS',
            1:'Mac',
            2:'Win 95',
            3:'Windows',
            4:'Linux/UNIX',
            5:'OS2',
            6:'NT',
            7:'PC 4',
            8:'VT 100',
           98:'--',
           99:'Other'
        }[value]
    
    def translate_sex(self, value):
        return {
             0: 'f',
             1: 'm',
            98: '--'
        }[value]
    
    def translate_web_development(self, value):
        return {
             1: 'Yes',
             2: 'No',
            98: '--'
        }[value]
    
    def __absorb(self, elements):
        if len(elements) < 72:
            raise ValueError('Not enough data points: %d (required: %d)' % (len(elements), 72))
        elif 72 < len(elements):
            raise ValueError('Too many data points: %d (required: %d)' % (len(elements), 72))
        
        self.age = int(elements[age_index])
        if 0 < self.age and self.age < 5:
            raise ValueError('suspicious data: age = %d' % self.age)
        elif 99 == self.age or 0 == self.age:
            self.age = random.randint(5, 80)
        (self.country, self.state) = self.translate_location(int(elements[location_index]))
        
        self.educational_attainment = self.translate_education(int(elements[education_attainment_index]))
        try:
            self.gender = self.translate_sex(int(elements[gender_index]))
        except KeyError:
            raise ValueError('suspicious data: gender = %d' % int(elements[gender_index]))
        self.occupation = self.translate_occupation(int(elements[occupation_index]))
        self.primary_computing_platform = self.translate_platform(int(elements[primary_computing_platform_index]))
        self.web_page_creation = self.translate_web_development(int(elements[web_page_creation_index]))
        self.years_on_internet = self.translate_internet_usage(int(elements[years_on_internet_index]))
        self.id = int(elements[id_index])
    
    def __lt__(self, other):
        return self.id < other.id
    
    def __repr__(self):
        return '[%d] age: %d, sex: %s, (%s, %s)' % (self.id, self.age, self.gender, self.country, self.state)
        
    def __str__(self):
        return '[%5d] age: %2d, sex: %s, loc: (%16s, %-16s), job: %-16s, edu: %-16s, os: %-12s, dev: %-3s, net: %-16s' % (self.id, self.age,
                self.gender, self.country, self.state, self.occupation, self.educational_attainment,
                self.primary_computing_platform, self.web_page_creation, self.years_on_internet)


def main():
    verbose = False
    with open(os.path.expanduser('~/Downloads/internet_usage_data/final_general.dat')) as input_file:
        records = []
        for line in input_file:
            try:
                records.append(InternetUsageRecord(line))
            except Exception as error:
                if verbose:
                    sys.stderr.write('skipping record: %s\n' % str(error))
        print 'record count:', len(records)
        for record in sorted(records):
            print record
    
if __name__ == '__main__':
    main()